/**
 * author: zhou
 * date: 2017.10.18
 * description: 封装query查询为promise类型,接受一个query参数,返回query执行后的结果
 * 
 */

const logger = require('../../utils/log');

const mongoPromise = (ctx, query) => {
    try {
        const result = new Promise((resolve, reject) => {
            query.exec().then((docs) => {
                resolve(docs);
            },
            (error) => {
                reject(error);
            });
        });
        return result;
    } catch (error) {
        logger.logError(`执行mongodb查询时发生错误! error: ${error}`, 0);
        ctx.body = { success: false, message: '抱歉,服务器异常,请稍后重试', state: 10003 };
    }
};

module.exports = mongoPromise;